package com.pan.server.controller.Admin;

import com.github.pagehelper.PageInfo;
import com.pan.common.result.Result;
import com.pan.pojo.dto.In;
import com.pan.pojo.dto.PageDTO;
import com.pan.pojo.dto.UpdateUserDTO;
import com.pan.pojo.vo.UserAdminVO;
import com.pan.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/adminUser")
@Slf4j
@Api(tags = "管理用户控制器")
@RestController
@CrossOrigin("http://localhost:7070")
public class AdminUsersController {
    @Autowired
    private UserService userService;

    /**
     * 获取所有用户
     *
     * @param pageDTO
     */
    @PostMapping(value = "/getAllUsers")
    @ApiOperation("获取所有用户")
    public Result<PageInfo<UserAdminVO>> getAllUsers(@RequestBody PageDTO pageDTO) {
        log.info("获取所有用户...");

        PageInfo<UserAdminVO> pageInfo = userService.getAllUsers(pageDTO.getPageNum(), pageDTO.getPageSize());

        return Result.success(pageInfo);
    }

    /**
     * 删除用户
     * @param in
     * @return
     */
    @PostMapping(value = "/deleteUser")
    @ApiOperation("删除用户")
    public Result<String> deleteUser(@RequestBody In in) {
        log.info("删除用户...");

        userService.deleteUser(in.getOrderId());

        return Result.success("删除用户成功");
    }

    /**
     *
     * @param updateUserDTO
     * @return
     */
    @PostMapping(value = "/updateUser")
    @ApiOperation("更新用户")
    public Result<String> updateUser(@RequestBody UpdateUserDTO updateUserDTO) {
        log.info("更新用户...");

        userService.updateUserAdmin(updateUserDTO.getUserId(), updateUserDTO.getPermissionId());

        return Result.success("更新用户成功");
    }
}